#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

/*
	把一个十进制的数转换为一个二进制的数，
	例如 9 转换为二进制是 1001，可以使用栈来实现。
*/

typedef struct node
{
	int data;
	struct node *up;
	struct node *down;
} Stack;

Stack *create(int n)
{
	int i;
	Stack *bottom = (Stack *)malloc(sizeof(Stack));
	bottom->down = NULL;
	Stack *top = bottom;
	for (i = 0; i < n; i++)
	{
		top->up = (Stack *)malloc(sizeof(Stack));
		top->up->down = top;
		top = top->up;
	}
	top->up = NULL;
	return bottom->up;
};

void destroy(Stack *p)
{
	Stack *up = p;
	while (up->up != NULL)
	{
		up = up->up;
	}
	while (up->down != NULL)
	{
		up = up->down;
		free(up->up);
	}
	free(up);
}

int push(Stack **top, int data)
{
	if ((*top)->up != NULL)
	{
		*top = (*top)->up;
		(*top)->data = data;
		return 0;
	}
	else
	{
		return -1;
	}
}

int pop(Stack **top)
{
	assert((*top)->down != NULL);
	int data = (*top)->data;
	(*top) = (*top)->down;
	return data;
}

int main()
{
	int n;
	printf("Enter an integer: ");
	scanf("%d", &n);
	Stack *stack = create(8 * sizeof(int));
	int bits = 0;
	while (n != 0)
	{
		push(&stack, n % 2);
		n /= 2;
		bits++;
	}
	while (bits > 0)
	{
		printf("%d", pop(&stack));
		bits--;
	}
	putchar('\n');
	destroy(stack);
	return 0;
}
